package com.hy.filter;


import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter(value = "/*", filterName = "HTMLFilter")
public class HTMLFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("HTMLFilter=========>");
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest)req;
        HttpServletResponse response = (HttpServletResponse)resp;

        HTMLHttpServletRequest htmlHttpServletRequest = new HTMLHttpServletRequest(request);

        chain.doFilter(htmlHttpServletRequest, response);
    }

    @Override
    public void destroy() {

    }

    private class HTMLHttpServletRequest extends HttpServletRequestWrapper{
        public HTMLHttpServletRequest(HttpServletRequest request) {
            super(request);
        }
        @Override
        public String getParameter(String name) {
            String value = super.getParameter(name);

            if(value == null)
                return null;

            //如果不为空
            value = htmlFilter(value);

            return value;
        }

        private String htmlFilter(String message) {
            if(message == null)
                return null;

            //将字符串转成字节数组
            char[] content = message.toCharArray();

//          char[] chars2 = new char[message.length()];
//          message.getChars();

            StringBuffer sb = new StringBuffer();

            for(int i=0,len = content.length ;i<len;i++){
                //content 中的内容 <input>xxxxxx</input>
                switch (content[i]){
                    case '>':
                        sb.append("&gt;");
                        break;
                    case '<':
                        sb.append("&lt;");
                        break;
                    case '&':
                        sb.append("&amp;");
                        break;
                    case '"':
                        sb.append("&quot;");
                        break;
                    default:
                        sb.append(content[i]);
                        break;
                }

//                if(content[i] == '>')
//                    sb.append("&gt;");
//                else if(content[i] == '<')
//                    sb.append("&lt;");
//                else if(content[i]=='&')
//                    sb.append("&amp");
//                else if(content[i]=='"')
//                    sb.append("&quot;");
//                else
//                    sb.append(content[i]);
            }

            return sb.toString() ;
        }
    }
}
